//
//  ClientiInterventoViewController.m
//  InterventixApp
//
//  Created by Diego Falcinelli on 20/06/13.
//
//

#import "ClientiInterventoViewController.h"
#import "Syncronize.h"
#import "ClienteCD.h"
#import "InfoClienteViewController.h"
#import "Util.h"

@interface ClientiInterventoViewController ()

@end

@implementation ClientiInterventoViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.clienteSelect = nil;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (void)viewWillAppear:(BOOL)animated{
    self.array = [[NSMutableArray alloc] initWithArray:[self.coreDataManager getClienti]];
    self.search = [[NSMutableArray alloc] initWithArray:self.array];
    [self.table reloadData];
}

-(IBAction) indietro{
	[[self navigationController] popViewControllerAnimated:YES];
}

-(IBAction) nuovo{
    ClienteCD *cliente = [NSEntityDescription
                          insertNewObjectForEntityForName: @"ClienteCD"
                          inManagedObjectContext:self.coreDataManager.managedObjectContext];
    cliente.idcliente = [Util getNextClienteId];
    cliente.buffer = @0;
    cliente.cestinato = @0;
    cliente.cancellato = @0;
    cliente.revisione = @-1;
    
    InfoClienteViewController *open = [self.storyboard instantiateViewControllerWithIdentifier:@"InfoClienteViewController"];
    open.intervento = self.intervento;
    open.cliente = cliente;
    open.coreDataManager = self.coreDataManager;
    open.user = self.user;
    open.add = true;
    open.clienteSelect = self.clienteSelect;
    [self.navigationController pushViewController:open animated:TRUE];
}


// TABELLA

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.search count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
    
    ClienteCD *cli = [self.search objectAtIndex:indexPath.row];
    
    if([cli.buffer isEqual: @1]){
        NSLog(@"BUFFER %@", cli.buffer);
        [Util setCellModified:cell];
    }else{
        [Util setCellDefault:cell];
    }
    
    cell.textLabel.text = cli.nominativo;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ - %@",cli.citta,cli.indirizzo];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    //[Util setCellModified:cell];
    
    return cell;
}

// SWIPE ROW
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return YES if you want the specified item to be editable.
    return YES;
}
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.navigationController setNavigationBarHidden:YES animated:YES];
        ClienteCD *c = [self.search objectAtIndex:indexPath.row];
        
        InfoClienteViewController *open = [self.storyboard instantiateViewControllerWithIdentifier:@"InfoClienteViewController"];
        open.intervento = self.intervento;
        open.cliente = c;
        open.clienteSelect = self.clienteSelect;
        open.coreDataManager = self.coreDataManager;
        open.user = self.user;
        open.add = false;
        open.hashCLient = [CoreDataManager hashcodeCliente:c];
        [self.navigationController pushViewController:open animated:TRUE];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"Modifica";
}
// END SWIPE ROW

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
    [self filterContentForSearchText:searchString scope:
     [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
    
    // Return YES to cause the search result table view to be reloaded.
    return YES;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)saearchBar {
    [self.search removeAllObjects];
    [self.search addObjectsFromArray: self.array];
}

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope{
    self.search = [[NSMutableArray alloc] initWithArray:[self.coreDataManager likeClienti:searchText]];
    NSLog(@"%f",self.searchDisplayController.searchResultsTableView.frame.origin.y);
    
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0) {
        self.searchDisplayController.searchResultsTableView.frame =
        CGRectMake(
                   0,
                   self.searchBar.frame.size.height + 76,
                   self.searchDisplayController.searchResultsTableView.frame.size.width,
                   self.searchDisplayController.searchResultsTableView.frame.size.height
                   );
    }
}


// CellHeight
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 50;
}

// CLICK
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ClienteCD *cli = [self.search objectAtIndex:indexPath.row];
    self.intervento.cliente = cli;
    [[self navigationController] popViewControllerAnimated:YES];
}






- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    self.inSearchMode = YES;
}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    self.inSearchMode = NO;
}





@end
